import {
  Controller,
  Get,
  Param,
  Delete,
  Query,
  Post,
  Body,
} from '@nestjs/common';
import { CustomerSimpleManageService } from './customer-simple-manage.service';

@Controller('customersimplemanage')
export class CustomerSimpleManageController {
  constructor(
    private readonly customerSimpleManageService: CustomerSimpleManageService,
  ) {}

  @Get()
  findAll() {
    return this.customerSimpleManageService.findAll();
  }

  @Get('search')
  async searchByKeywordGet(
    @Query('keyword') keyword: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    const pageNum = page ? Math.max(1, parseInt(page, 10) || 1) : 1;
    const pageSize = limit
      ? Math.max(1, Math.min(parseInt(limit, 10) || 50, 1000))
      : 50;

    const result = await this.customerSimpleManageService.searchByKeyword(
      keyword || '',
      pageNum,
      pageSize,
    );
    return result;
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.customerSimpleManageService.findOne(+id);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.customerSimpleManageService.remove(+id);
  }

  @Post('search')
  async searchByKeyword(
    @Body('keyword') keyword: string,
    @Body('page') page: number = 1,
    @Body('limit') limit: number = 50,
  ) {
    const result = await this.customerSimpleManageService.searchByKeyword(
      keyword,
      page,
      limit,
    );
    return result;
  }
}
